/**
 * 
 */
package org.xlab.baseframework.test.platform.webservice.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.PermissionDeniedException;
import org.xlab.baseframework.exception.SystemException;
import org.xlab.baseframework.test.platform.domain.SystemSession;
import org.xlab.baseframework.test.platform.service.api.ISystemSessionService;
import org.xlab.baseframework.test.platform.webservice.api.ISystemWebService;

/**
 * @author makubex
 * 
 */
@WebService(endpointInterface = "org.xlab.baseframework.test.platform.webservice.api.ISystemWebService")
public class SystemWebServiceImpl implements ISystemWebService {

	private static final Logger log = Logger
			.getLogger(SystemWebServiceImpl.class);

	private ISystemSessionService systemSessionService;

	/**
	 * @return the systemSessionService
	 */
	public ISystemSessionService getSystemSessionService() {
		return systemSessionService;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.ISystemWebService#login(java.lang.String,
	 *      java.lang.String)
	 */
	public String login(String userName, String password, String deviceId)
			throws PermissionDeniedException {
		if (userName.equals(password)) {
			String sessionId = UUID.randomUUID().toString();
			SystemSession sess = new SystemSession();
			sess.setSessionId(sessionId);
			sess.setCreatedBy(userName);
			sess.setLastUpdatedBy(userName);
			Date current = new Date();
			sess.setCreatedTime(current);
			sess.setLastUpdatedTime(current);
			sess.setDeletedInd(EnumDeletedIndentifying.N);
			sess.setDeviceId(deviceId);
			this.systemSessionService.save(sess);
			log.debug("Login successed! Session ID is: " + sessionId
					+ ", device ID is: " + deviceId);
			return sessionId;
		} else {
			log.warn("User name does not equals user password, please try again!");
			throw new PermissionDeniedException(
					"User name does not equals user password, please try again!");
		}
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.ISystemWebService#
	 *      logout(java.lang.String, java.lang.String)
	 */
	public void logout(String sessionId, String deviceId)
			throws PermissionDeniedException, SystemException {
		List<SystemSession> allSession = this.systemSessionService
				.findAllByProperty("sessionId", sessionId);
		if (allSession == null || allSession.isEmpty() || allSession.size() > 1) {
			throw new SystemException("Error occurred!");
		} else {
			SystemSession session = allSession.get(0);
			if (session.getDeviceId().equals(deviceId)
					&& session.getDeletedInd() == EnumDeletedIndentifying.N) {
				session.setLastUpdatedTime(new Date());
				session.setDeletedInd(EnumDeletedIndentifying.Y);
				this.systemSessionService.update(session);
				log.debug("Device: " + deviceId + " with session: " + sessionId
						+ " logout!");
			} else {
				log.warn("Device: " + deviceId
						+ " is using a session which does not belong to it!");
			}
		}
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.ISystemWebService#ping(java.lang.String)
	 */
	public void ping(String sessionId, String deviceId) throws SystemException {
		List<SystemSession> allSession = this.systemSessionService
				.findAllByProperty("sessionId", sessionId);
		if (allSession == null || allSession.isEmpty() || allSession.size() > 1) {
			throw new SystemException("Error occurred!");
		} else {
			SystemSession session = allSession.get(0);
			if (session.getDeviceId().equals(deviceId)) {
				session.setLastUpdatedTime(new Date());
				this.systemSessionService.update(session);
				log.debug("Sending response to device: " + deviceId);
			} else {
				log.warn("Device: " + deviceId
						+ " is using a session which does not belong to it!");
			}
		}
	}

	/**
	 * @param systemSessionService
	 *            the systemSessionService to set
	 */
	public void setSystemSessionService(ISystemSessionService systemSessionService) {
		this.systemSessionService = systemSessionService;
	}

}
